****TABLE 2****

cd "$main_dir/output/tables/main"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc)

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample gallup_sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.
gen gallup_sample = 1 if born_country==1 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=. & survey=="Gallup"
replace gallup_sample = 1 if total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=. & survey=="Gallup" & (born_country!=0) & survey_year==2006

//--------------------------------------------------------
//Create Table 2
//--------------------------------------------------------

//Column 1
qui reghdfe trust_others wgrowth_rate_1 yob_ln_gdppc if sample==1 & trust_govt!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
cap drop z*
local varlist trust_others wgrowth_rate_1 yob_ln_gdppc
foreach var of local varlist {
	egen z`var' = std(`var') if e(sample)==1
}
reghdfe ztrust_others zwgrowth_rate_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_2.doc, replace dec(3) nocons addtext(Country FE, Yes, ///
														 Survey Year FE x Survey FE, Yes, ///
														 Subcontinent FE x Cohort FE, Yes, ///
														 Survey, Harmonized) keep(zwgrowth_rate_1) 

//Column 2
qui reghdfe trust_religious_organizations wgrowth_rate_1 yob_ln_gdppc if sample==1 & trust_govt!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
cap drop z*
local varlist trust_religious_organizations wgrowth_rate_1 yob_ln_gdppc
foreach var of local varlist {
	egen z`var' = std(`var') if e(sample)==1
}
reghdfe ztrust_religious_organizations zwgrowth_rate_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_2.doc, append dec(3) nocons addtext(Country FE, Yes, ///
														Survey Year FE x Survey FE, Yes, ///
														Subcontinent FE x Cohort FE, Yes, ///
														Survey, Harmonized) keep(zwgrowth_rate_1) 

//Column 3
qui reghdfe trust_state wgrowth_rate_1 yob_ln_gdppc if sample==1 & trust_govt!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
cap drop z*
local varlist trust_state wgrowth_rate_1 yob_ln_gdppc
foreach var of local varlist {
	egen z`var' = std(`var') if e(sample)==1
}
reghdfe ztrust_state zwgrowth_rate_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_2.doc, append dec(3) nocons addtext(Country FE, Yes, ///
														Survey Year FE x Survey FE, Yes, ///
														Subcontinent FE x Cohort FE, Yes, ///
														Survey, Harmonized) keep(zwgrowth_rate_1)

//Column 4
qui reghdfe govt_corrupt wgrowth_rate_1 yob_ln_gdppc if sample==1 & trust_govt!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
cap drop z*
local varlist govt_corrupt wgrowth_rate_1 yob_ln_gdppc
foreach var of local varlist {
	egen z`var' = std(`var') if e(sample)==1
}
reghdfe zgovt_corrupt zwgrowth_rate_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_2.doc, append dec(3) nocons addtext(Country FE, Yes, ///
														Survey Year FE x Survey FE, Yes, ///
														Subcontinent FE x Cohort FE, Yes, ///
														Survey, Gallup) keep(zwgrowth_rate_1)

//Column 5
qui reghdfe feelings_income wgrowth_rate_1 yob_ln_gdppc if sample==1 & trust_govt!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
cap drop z*
local varlist feelings_income wgrowth_rate_1 yob_ln_gdppc
foreach var of local varlist {
	egen z`var' = std(`var') if e(sample)==1
}
reghdfe zfeelings_income zwgrowth_rate_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_2.doc, append dec(3) nocons addtext(Country FE, Yes, ///
														Survey Year FE x Survey FE, Yes, ///
														Subcontinent FE x Cohort FE, Yes, ///
														Survey, Gallup) keep(zwgrowth_rate_1)

//Column 6
qui reghdfe standard_living wgrowth_rate_1 yob_ln_gdppc if sample==1 & trust_govt!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
cap drop z*
local varlist standard_living wgrowth_rate_1 yob_ln_gdppc
foreach var of local varlist {
	egen z`var' = std(`var') if e(sample)==1
}
reghdfe zstandard_living zwgrowth_rate_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_2.doc, append dec(3) nocons addtext(Country FE, Yes, ///
														Survey Year FE x Survey FE, Yes, ///
														Subcontinent FE x Cohort FE, Yes, ///
														Survey, Gallup) keep(zwgrowth_rate_1)
